/*
 * Balloon.fx
 *
 * Created on 17/12/2008, 18:20:24
 */

package socialcampx;

import java.lang.Math;
import javafx.scene.Cursor;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.ShapeSubtract;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

/**
 * @author 00412804581
 */

public class Balloon extends CustomNode {

    /* (cx,cy) center of the balloon */
    public var cx: Number = 100 on replace {
        distance = Math.sqrt(toX * toX + toY * toY);
    }
    public var cy: Number = 100 on replace {
        distance = Math.sqrt(toX * toX + toY * toY);
    }

    /* (toX, toY) point where balloon points at */
    public var toX: Number = cx on replace {
        distance = Math.sqrt(toX * toX + toY * toY);
    }
    public var toY: Number = cy on replace {
        distance = Math.sqrt(toX * toX + toY * toY);
    }

    /* what is writted in the balloon */
    public var text: String = "balloon";

    /* font for the text */
    public var font: Font = Font {
        size: 24
        //style: Font.
    }

    /* Distance between (cx,cy) and (toX, toY) */
    var distance: Number;

    public var width: Number = 200;

    /* Text inside the balloon */
    var label = Text {
        font: bind font
        content: bind text
    }

    /* place the label correctly based on text */
    init {
        label.x = 0;//-label.wrappingWidth / 2;
        label.y = label.font.size / 2;
    }

    /* ballon body */
    var body = ShapeSubtract{
        fill: Color.WHITE
        stroke: Color.BLACK
        blocksMouse: true
        a: bind [
            Ellipse {
                radiusX: bind width / 2 + 20
                radiusY: bind font.size * 2
            },
            Polygon {
                points: [
                    10 * ( - toY / distance),
                    10 * (toX / distance),
                    10 * (toY / distance),
                    10 * ( - toX / distance),
                    toX,
                    toY
                ]
            }
        ]
    }


    public override function create(): Node {
        return Group {
            cursor: Cursor.HAND
            content: [ body, label]
            translateX: bind cx
            translateY: bind cy
        }
    }
}